package com.lh.page.service.impl;

import com.lh.item.bo.SpuBo;
import com.lh.item.pojo.*;
import com.lh.page.client.BrandClient;
import com.lh.page.client.CategoryClient;
import com.lh.page.client.GoodsClient;
import com.lh.page.client.SpecificationClient;
import com.lh.page.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author liuhe
 * @date 2020/06/29
 */
@Service
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private SpecificationClient specificationClient;

    @Autowired
    private BrandClient brandClient;

    @Autowired
    private CategoryClient categoryClient;

    /**
     * 查询spu\spudetail\sku集合
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> loadModel(Long id) {
        Map<String, Object> map = new HashMap<>();
        // spu
        SpuBo spuBo = this.goodsClient.querySpuById(id);

        // spu detail
        SpuDetailDo spuDetail = this.goodsClient.querySpuDetailBySpuId(id);
        // spu特有属性，如{"4":["金","粉","灰","银"],"12":["4GB"],"13":["32GB"]}

        // sku列表
        List<SkuDo> skuDos = this.goodsClient.querySkuListBySpuId(id);

        // spu规格组
        List<SpecGroup> groups = this.specificationClient.querySpecGroupsByCid(spuBo.getCid3());

        // 查询特殊的规格参数
        List<SpecParam> specParams = this.specificationClient.querySpecParam(null, spuBo.getCid3(), null, false);
        HashMap<Long, String> paramMap = new HashMap<>();
        specParams.forEach(specParam -> {
            paramMap.put(specParam.getId(), specParam.getName());
        });

        // 查询分类
        List<Long> cids = Arrays.asList(spuBo.getCid1(), spuBo.getCid2(), spuBo.getCid3());
        List<String> names = this.categoryClient.queryNameByIds(cids);
        List<Map<String, Object>> categories = new ArrayList<>();
        for (int i = 0; i < cids.size(); i++) {
            Map<String, Object> categoryMap = new HashMap<>();
            categoryMap.put("id", cids.get(i));
            categoryMap.put("name", names.get(i));
            categories.add(categoryMap);
        }

        // 查询品牌信息
        BrandDo brandDo = this.brandClient.queryBrandByIds(Collections.singletonList(spuBo.getBrandId())).get(0);

        map.put("spu", spuBo);
        map.put("spuDetail", spuDetail);
        map.put("skus", skuDos);
        map.put("brand", brandDo);
        map.put("categories", categories);
        map.put("groups",groups);
        map.put("paramMap",paramMap);

        return map;

    }
}
